package com.test.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import msg.chestionar.ejb.bi.ChestionarBI;

import com.msg.chestionar.to.ChestionarTO;
import com.msg.chestionar.to.IntrebareOptiuneTO;
import com.msg.chestionar.to.IntrebareTO;

@ManagedBean(name = "editBean")
@SessionScoped
public class EditBean implements Serializable {
	private static final long serialVersionUID = -4759076671753497857L;

	private boolean activ;
	private String nume;

	private ChestionarTO chestionar;

	@EJB
	private ChestionarBI chestionarBI;

	@ManagedProperty(value = "#{editIntrebareBean}")
	private EditIntrebareBean editIntrebareBean;

	public ChestionarTO getChestionar() {

		if (chestionar == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			String chestionarId = (String) context.getExternalContext()
					.getRequestParameterMap().get("chestionarSelected");
			if (chestionarId != null)
				this.chestionar = chestionarBI.getChestionar(Long
						.valueOf(chestionarId));
			else
				this.chestionar = new ChestionarTO();
		}
		return chestionar;
	}


	public String addIntrebare() {
		IntrebareTO inntr = new IntrebareTO();
		
		int nrOrder=0;
		for (IntrebareTO to: chestionar.getIntrebari()){
			if (to.getOrderNo()>nrOrder){
				nrOrder = to.getOrderNo();
			}
		}
		inntr.setOrderNo(nrOrder+1);
		this.chestionar.getIntrebari().add(inntr);
		editIntrebareBean.setIntrebare(inntr);
		editIntrebareBean.setText("");
		editIntrebareBean.setOptiuni(new ArrayList<IntrebareOptiuneTO>());
		editIntrebareBean.setOptiuneText("");
		return "editare_intrebare";
	}

	public String edit() {

		FacesContext context = FacesContext.getCurrentInstance();
		String nrOrdineIntrebare = (String) context.getExternalContext()
				.getRequestParameterMap().get("intrebareSelected");
		int nrOrdineInt = Integer.valueOf(nrOrdineIntrebare);
		
		IntrebareTO intrebareTO = getIntrebareDupaNrOrdine(nrOrdineInt);
		
		editIntrebareBean.setIntrebare(intrebareTO);
		editIntrebareBean.setText(intrebareTO.getText());
		editIntrebareBean.setTip(intrebareTO.getTip());
		
		List<IntrebareOptiuneTO> optiuni = new ArrayList<IntrebareOptiuneTO>();
		
	    for(IntrebareOptiuneTO optiuneTO : intrebareTO.getOptiuni())
	    {
	    	IntrebareOptiuneTO copyTO = new IntrebareOptiuneTO();
	    	copyTO.setId(optiuneTO.getId());
	    	copyTO.setE_cu_comentariu(optiuneTO.getE_cu_comentariu());
	    	copyTO.setText(optiuneTO.getText());
	    	copyTO.setMarkedForDelete(optiuneTO.getMarkedForDelete());
	    	optiuni.add(copyTO);
	    	
	    }
		//se  itereaza lista de optiuni de pe intrebare
		// se copiaza fiecare optiune intr-o optiune nou
		// se creaza o lista noua
		// optiunea noua se adauga listei noi de optiuni de pe Bean
		
		editIntrebareBean.setOptiuni(optiuni);
		editIntrebareBean.setComentariu(intrebareTO.getWithComment());

		return "editare_intrebare";
	}
	public String delete() {

		FacesContext context = FacesContext.getCurrentInstance();

		String nrOrdineIntrebare = (String) context.getExternalContext()
				.getRequestParameterMap().get("intrebareSelected");
		int nrOrdineInt = Integer.valueOf(nrOrdineIntrebare);

		IntrebareTO intrebareTO = getIntrebareDupaNrOrdine(nrOrdineInt);
		
		if (intrebareTO.getId() != null) {
			intrebareTO.setMarkedForDelete(true);
		} else {
			chestionar.getIntrebari().remove(intrebareTO);
		}

		return "editare_chestionar";

	}


	private IntrebareTO getIntrebareDupaNrOrdine(int nrOrdineInt) {
		IntrebareTO intrebareTO = null;
		for(IntrebareTO intrebare : chestionar.getIntrebari()) {
			if(intrebare.getOrderNo() == nrOrdineInt) {
				intrebareTO = intrebare;
			}
		}
		return intrebareTO;
	}

	public String save() {
		this.chestionar.setNume(this.getNume());
		this.chestionar.setActiv(this.getActiv());

		this.chestionar.setCreat_de("admin");

		chestionar.setActiv(this.getActiv());

		System.out.println("Name: " + this.chestionar.getNume());
		System.out.println("Activ: " + this.chestionar.getActiv());

		chestionarBI.saveChestionar(chestionar);

		// chestionarBI.addChestionar(chest);

		return "chestionar_salvat";
	}

	public void setNume(String nume) {
		this.nume = nume;
	}

	public String getNume() {
		return nume;
	}
	
	public void setChestionar(ChestionarTO chestionar) {
		this.chestionar = chestionar;
	}

	public void setEditIntrebareBean(EditIntrebareBean editIntrebareBean) {
		this.editIntrebareBean = editIntrebareBean;
	}
	
	public boolean getActiv() {
		return activ;
	}
	
	public void setActiv(boolean a) {
		this.activ = a;
	}

}